<?php

namespace App\Http\Controllers\Backend;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\AdminAuth;
use App\Services\Goods\SpecsService;
use App\Traits\VerifyRequestParam;
use Illuminate\Http\Request;
/**
 * 商品规格控制器
 *
 * @package App\Http\Controllers\H5
 * @author WangBin
 * @date 2021-06/11
 */
class SpecsController extends BaseController
{
    use VerifyRequestParam;

    public function __construct()
    {

    }

    /**
     * 用户登录获取token
     *
     * @param Request $request
     * @return
     */
    public function login(Request $request)
    {
        return $this->success(['token' => '111111111111111111', 'user' => []]);
    }

    /**
     * 获取规格详情
     *
     * @return
     * @throws
     */
    public function get()
    {
        $ret = SpecsService::getInstance()->get($this->verifyInteger('id'));
        return $this->success($ret);
    }

    /**
     * 保存规格
     *
     * @param Request $request
     * @throws \App\Exceptions\BusinessException
     * @return
     */
    public function update()
    {
        $data = [
            'spec_id' => $this->verifyInteger('spec_id', 0),
            'spec_name' => $this->verifyString('spec_name'),
            'spec_bn' => $this->verifyString('spec_bn'),
            'is_active' => $this->verifyInteger('is_active', 1),
            'intro' => $this->verifyString('intro', '')
        ];
        $ret = SpecsService::getInstance()->update($data);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::GOODS_SPEC_UPDATE_FAIL);
    }

    /**
     * 规格列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws
     */
    public function getList(Request $request)
    {
        $filter = [
            'spec_name' => $this->verifyString('spec_name', ''),
            'spec_bn' => $this->verifyString('spec_bn', ''),
            'limit' => $this->verifyInteger('limit', 20),
            'page' => $this->verifyInteger('page', 1) - 1,
        ];
        $ret = SpecsService::getInstance()->getList($filter);
        return $this->success($ret);
    }

    /**
     * 删除规格
     *
     * @throws \App\Exceptions\BusinessException
     * @return
     */
    public function delete()
    {
        $specId = $this->verifyString('id');
        $ret = SpecsService::getInstance()->delete($specId);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::GOODS_SPEC_DELETE_FAIL);
    }

    /**
     * 获取规格值列表
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function getValList()
    {
        $specId = $this->verifyInteger('id');
        $ret = SpecsService::getInstance()->getValList($specId);
        return $this->success($ret);
    }

    /**
     * 保存规格值
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function updateVal(Request $request)
    {
        $specId = $this->verifyInteger('spec_id');
        $vals = $request->input('vals');
        $ret = SpecsService::getInstance()->updateVals($specId, $vals);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::GOODS_SPEC_UPDATE_FAIL);
    }

    /**
     * 删除规格值
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function deleteVal()
    {
        $valueId = $this->verifyInteger('id');
        $ret = SpecsService::getInstance()->deleteVal($valueId);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::GOODS_SPEC_VAL_DELETE_FAIL);
    }

    /**
     * 根据规格ID获取规格配置值
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function getVal()
    {
        $specId = $this->verifyInteger('spec_id');
        $ret = SpecsService::getInstance()->getValList($specId);
        return $this->success($ret);
    }
}
